package homework_2;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.imageio.ImageIO;
public class imgCuting {
	public imgCuting(BufferedImage prevImg) throws IOException{
		/*
		 * * cuting argus: x,y is the left up coordinate; width and height.
		 */
		File file2=new File("resultCuting.jpg");
		Scanner scanner1 = new Scanner(System.in);
		System.out.println("input your cuting argument, original width is "
				+prevImg.getWidth()+", height is "+prevImg.getHeight()+" \nx:");
		int x= scanner1.nextInt();
		System.out.println("y: ");
		int y= scanner1.nextInt();
		System.out.println("width: ");
		int width= scanner1.nextInt();
		System.out.println("height: ");
		int height= scanner1.nextInt();
		int edgeX =0,edgeY = 0;
		edgeX = x+width;
		edgeY = y+height;
		if(x+width>= prevImg.getWidth())
			edgeX=prevImg.getWidth();
		if(y+height >= prevImg.getHeight())
			edgeY= prevImg.getHeight();
		int i,j,xx=0, yy=0;
		if (x+width > prevImg.getWidth())
			width = prevImg.getWidth()-x;
		if(y+height > prevImg.getHeight())
			height = prevImg.getHeight()-y;
		BufferedImage newImg= new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
		for( i= x; i<edgeX;++i,++xx)
			for( j=y; j < edgeY ;++yy,++j)
			{
				int rgb = prevImg.getRGB(i, j);
				newImg.setRGB(i-x, j-y, rgb);
			}
		ImageIO.write(newImg, "jpg", file2);
		System.out.println("Done!");
		scanner1.close();
	}
}
